type_safe provides zero overhead abstractions that use the C++ type system to prevent bugs.
Zero overhead abstractions here and in following mean abstractions that have no cost with optimizations enabled, but may lead to slightly lower runtime in debug mode, especially when assertions for this library are enabled.
The library features cannot really explained in the scope of this readme, I highly suggest that you check out the first and second blog post and the examples.
ts::integer<T> - a zero overhead wrapper over a built-in integer type
unsigned integers, enabling compiler optimizationsts::floating_point<T> - a zero overhead wrapper over a built-in floating point
ts::boolean - a zero overhead wrapper over bool
ts::uint32_t or ts::size_t that are either wrapper or built-in type depending on macro342_u32 or 0_usizets::object_ref<T> - a non-null pointerts::index_t and ts::distance_t - index and distance integer types with only a subset of operations availablets::array_ref<T> - non-null reference to contigous storagets::function_ref<T> - non-null reference to a functionts::flag - an improved flag type, better than a regular bool or ts::booleants::flag_set<Enum> - a set of flagsts::output_parameter<T> - an improved output parameter compared to the naive lvalue referencets::basic_optional<StoragePolicy> - a generic, improved std::optional that is fully monadic, also ts::optional<T> and ts::optional_ref<T> implementationsts::compact_optional implementation for no space overhead optionalsts::basic_variant<VariantPolicy, Types...> - a generic, improved std::variant, also ts::variant and ts::fallback_variant implementationsts::constrained_type<T, Constraint, Verifier> - a wrapper over some type that verifies that a certain constraint is always fulfilled
ts::constraints::* - predefined constraints like non_null, non_empty, …ts::tagged_type<T, Constraint> - constrained type without checking, useful for taggingts::bounded_type<T> - constrained type that ensures a value in a certain intervalts::clamped_type<T> - constrained type that clamps a value to ensure that it is in the certain intervalts::strong_typedef - a generic facility to create strong typedefs more easilyts::deferred_construction<T> - create an object without initializing it yetHeader-only, just copy the files in your project. You need to add the type_safe include directory to your include path as well as make debug_assert.hpp available. The repository is included as git submodule, simply run git submodule update --init and add external/debug_assert to the include path. You also need to enable C++11.
Behavior can be customized with the following macros:
TYPE_SAFE_ENABLE_ASSERTIONS (default is 1): whether or not assertions are enabled in this libraryTYPE_SAFE_ENABLE_WRAPPER (default is 1): whether or not the typedefs in type_safe/types.hpp use the wrapper classesTYPE_SAFE_ARITHMETIC_UB (default is 1): whether under/overflow in the better integer types is UB.If you’re using CMake there is the target type_safe available after you’ve called add_subdirectory(path/to/type_safe). Simply link this target to your target and it will setup everything automagically. For convenience the macros are also mapped to CMake options of the same name.
This project is greatly supported by my patrons. In particular thanks to the individual supporters:
And big thanks to the main contributors as well: